//
//  ZXColorExtension.swift
//  ExtensionTest
//
//  Created by ZX on 2025/2/6.
//

import UIKit

public protocol HexColorConvertible {
    func convertToHex() -> Int
    func convertToColor() -> UIColor
}

extension String: HexColorConvertible {
    public func convertToHex() -> Int {
        let hexString = self.trimmingCharacters(in: .whitespacesAndNewlines)
        let scanner = Scanner(string: hexString)
        
        if hexString.hasPrefix("#") {
            scanner.currentIndex = scanner.string.index(scanner.string.startIndex, offsetBy: 1)
        }
        var hexColor: UInt64 = 0
        scanner.scanHexInt64(&hexColor)
        return Int(hexColor)
    }
    public func convertToColor() -> UIColor {
        .init(self.convertToHex())
    }
}

extension Int: HexColorConvertible {
    public func convertToHex() -> Int {
        return self
    }
    public func convertToColor() -> UIColor {
        .init(convertToHex())
    }
}

extension UIColor : HexColorConvertible{
    public func convertToHex() -> Int {
        return 0
    }
    public func convertToColor() -> UIColor {
        return self
    }
}

public extension UIColor {
    
    convenience init(_ hex: HexColorConvertible, _ alpha: CGFloat = 1) {
        if let hex = hex as? UIColor {
            self.init(cgColor: hex.cgColor)
            return
        }
        let colorValue = hex.convertToHex()
        let red = CGFloat((colorValue & 0xFF0000) >> 16) / 255.0
        let green = CGFloat((colorValue & 0x00FF00) >> 8) / 255.0
        let blue = CGFloat(colorValue & 0x0000FF) / 255.0
        self.init(red: red, green: green, blue: blue, alpha: alpha)
    }
    func alpha(_ alpha: CGFloat) -> UIColor {
        return self.withAlphaComponent(alpha)
    }
}


@available(*, deprecated, message: "This function is deprecated. Use newFunction instead.")
public extension UIColor{
    static func c(_ hex: Any? , _ alpha: CGFloat = 1) -> UIColor {
        var color: Int = 0
        if let hex = hex as? String{
            color = self.toHex(str: hex)
        }else if let hex = hex as? Int{
            color = hex
        }
        else {
            return .black
        }
        
        return UIColor.c(hex: color , alpha: alpha)
    }
    
    static private func c(hex: Int, alpha: CGFloat) -> UIColor {
        let red = CGFloat((hex & 0xFF0000) >> 16) / 255.0
        let green = CGFloat((hex & 0x00FF00) >> 8) / 255.0
        let blue = CGFloat(hex & 0x0000FF) / 255.0
        return UIColor.init(red:red , green:green , blue:blue , alpha: alpha)
    }
    
    private static func toHex(str: String) -> Int {
        let hexString = str.trimmingCharacters(in: .whitespacesAndNewlines)
        let scanner = Scanner(string: hexString)
        
        if hexString.hasPrefix("#") {
            scanner.currentIndex = scanner.string.index(scanner.string.startIndex, offsetBy: 1)
        }
        var hexColor: UInt64 = 0
        scanner.scanHexInt64(&hexColor)
        return Int(hexColor)
    }
}
